#include "Comm.h"

class Solution {
public:
    bool checkInclusion(string s1, string s2) {
        int hash1[26] = { 0 };
        int hash2[26] = { 0 };

        for (char ch : s1)
            hash1[ch - 'a']++;

        int left = 0, right = 0;
        int count = 0;
        while (right < s2.size())
        {
            if (++hash2[s2[right] - 'a'] <= hash1[s2[right] - 'a'])
            {
                count++;
                if (count == s1.size()) return true;
            }

            while (hash2[s2[right] - 'a'] > hash1[s2[right] - 'a'])
            {
                if (--hash2[s2[left] - 'a'] < hash1[s2[left] - 'a'])
                    count--;
                ++left;
            }

            right++;
        }
        return false;
    }
};